import unittest,time
from re import search
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys

class A3mall(unittest.TestCase):

    #TestFixture测试固件
    def setUp(self) -> None:
        self.driver = webdriver.Firefox()
        self.driver.get('http://10.59.9.12:7001/')
        self.driver.maximize_window()


    def tearDown(self) -> None:
        time.sleep(3)
        self.driver.quit()

    #TestCase测试用例
    def test_A3mall_01_login(self):
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        time.sleep(3)
        # 断言1：断言文本：会员登陆
        dy1 = self.driver.find_element(By.CLASS_NAME, 'reg-new').text
        print("文本断言3，会员登陆:", dy1)
        time.sleep(2)
        # 用户名  密码
        self.driver.find_element(By.NAME,'username').send_keys('13811033962')
        time.sleep(3)
        self.driver.find_element(By.NAME, 'password').send_keys("123456")
        time.sleep(8)
        # 登录按钮
        self.driver.find_element(By.XPATH, '//*[@id="theForm"]/div[5]/a').click()
        time.sleep(5)
        # 断言2：断言文本：欢迎回来，13811033962
        dy2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print("文本断言2，欢迎回来，13811033962:", dy2)
        time.sleep(2)
        # 断言3：断言文本：账户余额：¥ 988389770.25  中的 988389770.25
        dy3 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]').text
        dy31 = dy3.replace('¥ ', '')
        print("文本断言3，账户余额:", dy31)
        time.sleep(2)

    def test_A3mall_02_search(self):
        #调用成功登录用例
        self.test_A3mall_01_login()
        time.sleep(5)
        #点击首页
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a').click()
        time.sleep(2)
        #输入框输入音乐耳机
        self.driver.find_element(By.XPATH,'//*[@id="searchForm"]/div[1]/input').send_keys('音乐耳机')
        self.driver.find_element(By.XPATH,'//*[@id="searchForm"]/div[2]/input').click()
        time.sleep(8)
        #断言 4：搜索音乐耳机 搜索列表 断言文本：搜索结果
        dy4=self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/em/a').text
        print("文本断言4:",dy4)
        #选择音乐耳机进入商品详情
        self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img').click()
        time.sleep(5)
        #断言5：商品详情，断言文本：音乐耳机
        dy5=self.driver.find_element(By.XPATH,'/html/body/div[4]/a[3]').text
        print("断言5:",dy5)
        #断言6：4商品价格，断言文本：商品价格
        dy6=self.driver.find_element(By.XPATH,'//*[@id="sell-price-box"]').text
        print("断言6",dy6)
        #购物车数量 清除后重新输入
        numa=self.driver.find_element(By.ID,'store_nums')
        numa.clear()
        numa.send_keys('2')
        #断言7：商品数量2
        dy7=numa.text
        print("断言7:",dy7)
        #点击立即购买
        self.driver.find_element(By.ID,'buy-cart').click()
        time.sleep(4)
        #断言8：立即购买，确认订单信息
        dy8=self.driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/ul/li[2]/em').text
        print("断言8：",dy8)
        #选择收货地址
        self.driver.find_element(By.ID,'address-801').click()
        #确认订单
        self.driver.find_element(By.ID,'order-button').click()
        time.sleep(4)
        #断言，购买成功：确认订单信息
        dy9=self.driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/span').text
        print("断言9：",dy9)

    #TestSuite测试套件
    #TestRunner测试执行